package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.model.MyAddress;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.SharedPreferencesUtil;
import com.luck.easyrecyclerview.sideslip.SwipeMenuAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：LoveCare
 * package：com.tongyu.luck.lovecare.adapter
 * email：893855882@qq.com
 * data：16/8/12
 */
public class AddressAdapter extends SwipeMenuAdapter<AddressAdapter.DefaultViewHolder> {
    private Context mContext;
    private List<MyAddress.DataBean> list = new ArrayList<>();
    private int address_type;
    private int type;
    private String p_id = "3";
    public OnDefault onDefault;
    public OnDelecteAddressListener onDelete;
    public OnItemClickListener mOnItemClickListener;

    public void setAddress_type(int address_type) {
        this.address_type = address_type;
    }

    public AddressAdapter(Context context, List<MyAddress.DataBean> list, OnDefault onDefault, OnDelecteAddressListener onDelect, int type) {
        this.mContext = context;
        this.onDefault = onDefault;
        this.list = list;
        this.type = type;
        this.onDelete = onDelect;
        p_id = (String) SharedPreferencesUtil.getData(mContext, HttpUtil.P_ID, "3");
    }

    public void setList(List<MyAddress.DataBean> list) {
        this.list = list;
    }


    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.address_item, parent, false);
    }

    @Override
    public DefaultViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new DefaultViewHolder(realContentView);
    }


    @Override
    public void onBindViewHolder(DefaultViewHolder holder, final int position) {
        final MyAddress.DataBean bean = list.get(position);
        String addressStr = bean.getAddress_str();
        String uname = bean.getUname();
        String mobile = bean.getMobile();
        String street = bean.getStreet();
        String province = bean.getProvince();
        holder.tv_phone.setText(mobile);
        int is_default = bean.getIs_default();
        String title = "";
        String titleAddress = "";
        if (address_type == 2) {
            // 仓库地址
            title = "负责人：";
            titleAddress = "仓库地址：";
            bean.setChecked(true);
            holder.tv_default.setVisibility(View.VISIBLE);
            holder.tv_is_location.setText("");
            holder.tv_name.setTextColor(ContextCompat.getColor(mContext, R.color.color_53));
            holder.tv_phone.setTextColor(ContextCompat.getColor(mContext, R.color.color_53));
        } else {
            // 个人地址
            title = "收货人：";
            titleAddress = "收货地址：";
            if (type == 2) {
                // 选择地址的时候 判断一下是否在配送范围内
//                if (province.equals(p_id)) {
                // 在配送范围内
                bean.setChecked(true);
                holder.tv_default.setVisibility(View.VISIBLE);
                holder.tv_is_location.setText("");
                holder.tv_name.setTextColor(ContextCompat.getColor(mContext, R.color.color_53));
                holder.tv_phone.setTextColor(ContextCompat.getColor(mContext, R.color.color_53));
//                } else {
//                    // 不在配送范围
//                    bean.setChecked(false);
//                    holder.tv_default.setVisibility(View.GONE);
//                    holder.tv_name.setTextColor(ContextCompat.getColor(mContext, R.color.tab_color_false));
//                    holder.tv_phone.setTextColor(ContextCompat.getColor(mContext, R.color.tab_color_false));
//                    holder.tv_is_location.setText("不在配送范围内");
//                }
            }
        }

        holder.tv_name.setText(title + uname);
        holder.tv_address.setText(titleAddress + addressStr + street);

        switch (is_default) {
            case 0:
                // 不默认
                holder.tv_default.setText("设为默认");
                holder.iv_select.setBackgroundResource(R.mipmap.select_def_1x);
                holder.tv_default.setTextColor(mContext.getResources().getColor(R.color.tab_color_false));
                break;
            case 1:
                // 默认
                holder.tv_default.setText("默认");
                holder.iv_select.setBackgroundResource(R.mipmap.select_sel_1x);
                holder.tv_default.setTextColor(mContext.getResources().getColor(R.color.tab_color_true));
                break;
        }
//        holder.tv_default.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                onDefault.onDefault(bean.getAddress_id(), position);
//            }
//        });
        holder.iv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onDefault.onDefault(bean.getAddress_id(), position);
            }
        });
        holder.tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onDelete.onDelectAddress(bean.getAddress_id(), position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class DefaultViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView tv_name, tv_phone, tv_address, tv_default, tv_is_location, tv_delete;
        ImageView iv_select;

        public DefaultViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            tv_is_location = (TextView) itemView.findViewById(R.id.tv_is_location);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_phone = (TextView) itemView.findViewById(R.id.tv_phone);
            tv_address = (TextView) itemView.findViewById(R.id.tv_address);
            tv_default = (TextView) itemView.findViewById(R.id.tv_default);
            tv_delete = (TextView) itemView.findViewById(R.id.tv_delete);
            iv_select = (ImageView) itemView.findViewById(R.id.iv_select);
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(getAdapterPosition(), v);
            }
        }
    }



    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnDefault {
        void onDefault(String address_id, int position);
    }

    public interface OnDelecteAddressListener {
        void onDelectAddress(String address_id, int position);
    }
}
